<template>
  <div class='container'>
    <el-card class="box-card">
      <el-form :inline="true" ref="form" :model="pagination" class="demo-form-inline" label-width="80px" size="small">
        <el-form-item label="关键字">
          <el-input v-model="pagination.keyword" placeholder="根据文章标题搜索"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="pagination.state" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClear">清除</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>

          <el-button style="margin-left: 430px;" type="success" @click="dialogFormVisible=true"><i class="el-icon-edit"></i>新增技巧</el-button>
        </el-form-item>
      </el-form>
      <div class="record">
        <el-alert v-if="alertText !== ''" :title="alertText" type="info" class="alert" :closable='false' show-icon></el-alert>
      </div>
      <!-- 列表 -->
      <el-table :data="tableData" :header-cell-style="{background:'#fafafa'}" style="width: 100%">
        <el-table-column label="序号" width="80" type="index">
        </el-table-column>
        <el-table-column prop="title" label="文章标题" width="320">
          <template #default="{row}">
            <span>{{row.title}}</span> <a v-if="row.videoURL" class="el-icon-film" @click="video(row.videoURL)"></a>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数">
        </el-table-column>
        <el-table-column prop="username" label="录入人">
        </el-table-column>
        <el-table-column prop="createTime" label="录入时间">
          <template #default="{row}">
            {{$dateFormat(row.createTime)}}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态" width="100">
          <template slot-scope="{row}">
            <span v-if="row.state === 1">已启用</span>
            <span v-else>已禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="178">
          <template slot-scope="{row}">
            <div v-if="row.state===1">
              <el-button type="text" @click="browse(row.id)">预览</el-button>
              <el-button type="text" @click="handleState(row)">禁用</el-button>
              <el-button type="text" :disabled="true">修改</el-button>
              <el-button type="text" :disabled="true">删除</el-button>
            </div>
            <div v-else>
              <el-button type="text" @click="browse(row.id)">预览</el-button>
              <el-button type="text" @click="handleState(row)">启用</el-button>
              <el-button type="text" :disabled="false" @click="mod(row.id)">修改</el-button>
              <el-button type="text" :disabled="false" @click="handleDle(row.id)">删除</el-button>
            </div>

          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="pagination"></div>
      <el-pagination v-if="counts" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[2,5, 10, 20, 50]" :page-size="pagination.pagesize" :currentPage="pagination.page" layout=" ->,  prev, pager, next,sizes, jumper" :total="counts">
      </el-pagination>
    </el-card>
    <!-- 新增技巧表单 -->
    <el-dialog title="新增文章" :visible.sync="dialogFormVisible" @close="Close">
      <el-form :model="addform" :rules="rules" ref="ruleForm">
        <el-form-item label="文章标题:" label-width="100px" prop="title">
          <el-input v-model="addform.title" autocomplete="off" width="80%" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章内容:" label-width="100px" prop="articleBody">
          <quill-editor v-model="addform.articleBody" />
        </el-form-item>
        <el-form-item label="视频地址:" label-width="100px" prop="videoURL">
          <el-input v-model="addform.videoURL" autocomplete="off" width="80%" placeholder="请输入文章标签"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="Close">取 消</el-button>
        <el-button type="primary" @click="addSkll">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改文章表单 -->
    <el-dialog title="修改文章" :visible.sync="modDialogFormVisible" @close="Close">
      <el-form :model="addform" :rules="rules" ref="ruleForm">
        <el-form-item label="文章标题:" label-width="100px" prop="title">
          <el-input v-model="addform.title" autocomplete="off" width="80%" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章内容:" label-width="100px" prop="articleBody">
          <quill-editor v-model="addform.articleBody" />
        </el-form-item>
        <el-form-item label="视频地址:" label-width="100px" prop="videoURL">
          <el-input v-model="addform.videoURL" autocomplete="off" width="80%" placeholder="请输入文章标签"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="Close">取 消</el-button>
        <el-button type="primary" @click="handleUpdata">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 预览文章详情 -->
    <el-dialog title="预览文章" :visible.sync="dialogVisible" width="52%">
      <h3 class="title">{{sklllist.title}}</h3>
      <span>{{sklllist.createTime}} </span> <span class="member">超级录入员</span><i class="el-icon-view"></i> <span>{{sklllist.id}}</span>
      <div v-html="sklllist.articleBody" class="content"></div>
    </el-dialog>
    <el-dialog class="vidShou" :append-to-body="true" :show-close="false" :visible.sync="customer" ref="vid">
      <el-button @click="closeVid" class="btn" type="info" icon="el-icon-close" circle></el-button>
      <video class="videos" :src="videoSrc" autoplay="" muted="" controls="" loop="" width="100%"></video>
    </el-dialog>

  </div>
</template>

<script>
// import { list } from '../../api/base/interview'
import { list, detail, changeState, remove, add, update } from '../../api/hmmm/articles'
export default {
  name: 'articles',
  async created () {
    this.gitList()
  },
  data () {
    return {
      // 当前页面条数长度
      items: null,
      // 标志位
      customer: false,
      //
      videoSrc: '',
      // 修改表单标志位
      modDialogFormVisible: false,
      // 新增表单验证
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: ['change', 'blur'] }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: ['change', 'blur'] }
        ]
      },
      // 修改状态参数
      state: {
        state: null,
        id: null
      },
      // 浏览数据
      sklllist: '',
      // 预览文章详情标志位
      dialogVisible: false,
      // 新增表单
      addform: {
        title: '',
        videoURL: null,
        articleBody: null,
        id: null
      },
      // 新增表单的标志位
      dialogFormVisible: false,
      // 分页
      pagination: {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      },
      // 列表
      tableData: [],
      counts: ''
      // 查询表单的数据
      // pagination: {
      //   keyword: null,
      //   state: null
      // }
    }
  },
  computed: {
    alertText () {
      return `共${this.counts}条记录`
    }
  },
  methods: {
    // 关闭视频
    closeVid () {
      this.customer = false
    },
    // 打开视频弹窗
    video (videoURL) {
      this.videoSrc = videoURL
      this.customer = true
    },
    // 修改表单
    handleUpdata () {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return
        try {
          const ret = await update({ id: this.id, ...this.addform })
          if (ret.status === 200) {
            this.$message.success('修改文章成功')
            this.modDialogFormVisible = false
            this.gitList()
          } else {
            this.$message.error('修改文章失败')
          }
        } catch {
          this.$message.error('修改文章失败')
        }
      })
    },
    // 获取文章填充到表单
    async mod (id) {
      try {
        const ret = await detail({ id: id })
        if (ret.status === 200) {
          this.addform.title = ret.data.title
          this.addform.articleBody = ret.data.articleBody
          this.addform.id = ret.data.id
          this.modDialogFormVisible = true
        } else {
          this.$message.error('获取文章失败')
        }
      } catch {
        this.$message.error('获取文章失败')
      }
    },
    // 关闭表单
    Close () {
      this.addform = {
        title: '',
        videoURL: null,
        articleBody: null,
        id: null
      }
      this.$refs.ruleForm.resetFields()
      this.modDialogFormVisible = false
      this.dialogFormVisible = false
    },
    // 删除文章
    handleDle (id) {
      this.$confirm('确认删除吗', '温馨提示', {
        type: 'warning'
      }).then(async () => {
        const ret = await remove({ id: id })
        if (ret.status === 200) {
          if (this.items === 1) {
            this.pagination.page -= 1
          }
          this.$message.success('删除文章成功')
          this.gitList()
        }
      }).catch(() => {
        this.$message.warning('取消删除')
      })
    },
    // 文章状态修改
    async handleState (row) {
      if (row.state === 1) {
        this.state.state = 0
        this.state.id = row.id
        try {
          const ret = await changeState(this.state)
          if (ret.status === 200) {
            // 刷新列表
            this.gitList()
          } else {
            this.$message.error('状态更新失败')
          }
        } catch {
          this.$message.error('状态更新失败')
        }
      }
      if (row.state === 0) {
        this.state.state = 1
        this.state.id = row.id
        try {
          const ret = await changeState(this.state)
          if (ret.status === 200) {
            // 刷新列表
            this.gitList()
          } else {
            this.$message.error('状态更新失败')
          }
        } catch {
          this.$message.error('状态更新失败')
        }
      }
    },
    // 预览
    async browse (id) {
      this.dialogVisible = true
      try {
        const ret = await detail({ id: id })
        if (ret.status === 200) {
          this.sklllist = ret.data
          console.log(this.sklllist)
        } else {
          this.$message.error('获取数据失败')
        }
      } catch {
        this.$message.error('获取数据失败')
      }
    },
    // 新增技巧
    addSkll () {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return
        try {
          const ret = await add(this.addform)
          if (ret.status === 200) {
            this.$message.success('添加文章成功')
            // 清空表单
            this.$refs.ruleForm.resetFields()
            // 刷新列表
            this.gitList()
            this.dialogFormVisible = false
            this.Close()
          } else {
            this.$message.error('添加文章失败')
          }
        } catch {
          this.$message.error('添加文章失败')
        }
      })
    },
    // 清楚表单的数据
    handleClear () {
      this.pagination.keyword = null
      this.pagination.state = null
    },
    // 初始化列表
    async gitList () {
      try {
        const ret = await list({ ...this.pagination })
        if (ret.status === 200) {
          this.tableData = ret.data.items
          this.counts = ret.data.counts
          this.items = ret.data.items.length
          // console.log(ret)
          // console.log(this.items)
        }
      } catch {
        this.$message.error('获取数据失败')
      }
    },
    // 搜索
    async onSubmit () {
      try {
        this.pagination.page = 1
        const ret = await list({ ...this.pagination })
        if (ret.status === 200) {
          this.pagination.page = 1
          // this.pagination.pagesize = 1
          this.tableData = ret.data.items
          this.counts = ret.data.counts
          this.items = ret.data.items.length

          // console.log(this.items)
        }
      } catch {
        this.$message.error('获取数据失败')
      }
    },
    handleCurrentChange (val) {
      this.pagination.page = val
      this.gitList()
    },
    // 每页条数发生改变时
    handleSizeChange (val) {
      // console.log(this.pagination)
      this.pagination.pagesize = val
      console.log(val)
      this.gitList()
    }
  }
}
</script>

<style scoped lang='scss'>
.box-card {
  margin: 10px 10px;
  .pagination {
    margin-top: 10px;
  }
}
.title {
  margin-top: 0px !important;
}
.member {
  padding: 0 10px !important;
}
.content {
  margin-top: 20px;
  padding: 10px 0;
  border-top: 1px dotted rgba(70, 68, 68, 0.664);
  background-color: rgb(240, 240, 240);
}
::v-deep .ql-container {
  height: 200px !important;
}
.el-icon-film {
  color: blue;
}
.record {
  margin-bottom: 20px;
}
.vidShou {
  position: fixed;

  ::v-deep .el-dialog__body {
    // background-color: rgba($color: #000000, $alpha: 0) !important;
    padding: 0;
    // background-color: rgba(0, 0, 0, 0.1);
    background-color: #000;
    // opacity: 0;
  }
  ::v-deep .el-dialog__header {
    display: none;
  }
  .btn {
    position: fixed;
    left: 50%;
    transform: translateX(-50%) !important;
    // position: relative;
    top: 10px;
    // left: 356px;
  }
  .videos {
    height: 100%;
  }
}

// .videoSrc {
//   position: absolute;
//   // background-color: red;
//   // display: ;
// }
</style>
